package com.seecoder.BlueWhale.controller;

import com.seecoder.BlueWhale.service.CouponGroupService;
import com.seecoder.BlueWhale.util.SecurityUtil;
import com.seecoder.BlueWhale.vo.CouponGroupVO;
import com.seecoder.BlueWhale.vo.DiscountParams;
import com.seecoder.BlueWhale.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/coupon_group")
@Slf4j
public class CouponGroupController {
    @Autowired
    CouponGroupService couponGroupService;
    @Autowired
    SecurityUtil securityUtil;

    @PostMapping("/ceo_create")
    public ResultVO<CouponGroupVO> ceoCreate(@RequestBody CouponGroupVO couponGroupVO) {
        log.info("CEO[{}] begins create coupon.", securityUtil.getUserId());
        ResultVO<CouponGroupVO> ans = ResultVO.buildSuccess(couponGroupService.ceoCreate(couponGroupVO));
        log.info("CEO[{}] create coupon ends.", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("/staff_create")
    public ResultVO<CouponGroupVO> creatLocal(@RequestBody CouponGroupVO couponGroupVO) {
        log.info("Staff[{}] begins create coupon.", securityUtil.getUserId());
        ResultVO<CouponGroupVO> ans = ResultVO.buildSuccess(couponGroupService.createLocal(couponGroupVO));
        log.info("Staff[{}] create coupon ends.", securityUtil.getUserId());
        return ans;
    }

    @GetMapping("/info")
    public ResultVO<List<CouponGroupVO>> info() {
        log.info("Get coupon group info.");
        ResultVO<List<CouponGroupVO>> ans = ResultVO.buildSuccess(couponGroupService.info());
        log.info("Get coupon group info ends.");
        return ans;
    }

    @PostMapping("/get_coupon_group")
    public ResultVO<CouponGroupVO> getCouponGroup(@RequestParam("id") int id) {
        log.info("Get coupon group info.");
        ResultVO<CouponGroupVO> ans = ResultVO.buildSuccess(couponGroupService.getCouponGroup(id));
        log.info("Get coupon group info ends.");
        return ans;
    }

    @PostMapping("/receive")
    public ResultVO<CouponGroupVO> receive(@RequestParam("id") int id) {
        log.info("User[{}] begins receive coupon.", securityUtil.getUserId());
        ResultVO<CouponGroupVO> ans = ResultVO.buildSuccess(couponGroupService.receive(id));
        log.info("User[{}] receive coupon ends.", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("discount")
    public ResultVO<Double> discount(@RequestBody DiscountParams discountParams) {
        log.info("User[{}] begins discount coupon.", securityUtil.getUserId());
        ResultVO<Double> ans = ResultVO.buildSuccess(couponGroupService.discount(discountParams));
        log.info("User[{}] discount coupon ends.", securityUtil.getUserId());
        return ans;
    }
}